/**
 * 
 */
package com.zito.web.www;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.downlove.database.POJO;
import com.downlove.template.freemarker.FreeMarkerEngine;
import com.zito.ErrMsgException;
import com.zito.bean.Article;
import com.zito.bean.Electronic;
import com.zito.bean.Electronicfiles;
import com.zito.manager.ArticlefilesManager;
import com.zito.manager.ArticlephotosManager;
import com.zito.manager.ContributeManager;
import com.zito.manager.ElectronicManager;
import com.zito.manager.ElectronicfilesManager;
import com.zito.manager.InteractSession;

/**
 * @author YunHai-Home
 * 
 */
public class Interact {

	private String TemplatePath = "/interact/";
	
	public InteractSession session() {
		return InteractSession.get();
	}
	
	/**
	 * 默认后台主界面
	 * 
	 * @param request
	 * @param response
	 */
	public void index(HttpServletRequest request, HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		FreeMarkerEngine.action(fm, TemplatePath + "main.html");
	}

	/**
	 * 管理员登录界面
	 * 
	 * @param request
	 * @param response
	 */
	public void login(HttpServletRequest request, HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		FreeMarkerEngine.action(fm, TemplatePath+"login.html");
	}
	
	/**
	 * 退出登录，返回登录界面
	 * 
	 * @param request
	 * @param response
	 */
	public void loginOut(HttpServletRequest request, HttpServletResponse response) {
		try {
			session().clearLogin();
			response.sendRedirect(request.getContextPath()+ TemplatePath +"login");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 框架左菜单部分
	 * 
	 * @param request
	 * @param response
	 */
	public void menu(HttpServletRequest request, HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		FreeMarkerEngine.action(fm, TemplatePath + "menu.html");
	}

	/**
	 * 管理后台默认欢迎页面
	 * 
	 * @param request
	 * @param response
	 */
	public void welcome(HttpServletRequest request, HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		FreeMarkerEngine.action(fm, TemplatePath + "welcome.html");
	}
	
	/**
	 * 在线交流
	 * 
	 * @param request
	 * @param response
	 */
	public void ChatOnline(HttpServletRequest request, HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		FreeMarkerEngine.action(fm, TemplatePath + "ChatOnline.html");
	}
	
	/**
	 * 
	 * 电子台账列表页面
	 */
	public void ElectronicList(HttpServletRequest request,
			HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		fm.put("manager", new ElectronicManager());
		FreeMarkerEngine.action(fm, TemplatePath + "ElectronicList.html");
	}
	
	/**
	 * 电子台账编辑页面
	 * @param request
	 * @param response
	 */
	public void ElectronicEdit(HttpServletRequest request,
			HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		ElectronicManager manager = new ElectronicManager();
		Electronic obj = new Electronic();
		ElectronicfilesManager file = new ElectronicfilesManager();
		List<? extends POJO> list = new ArrayList<Electronicfiles>();
		try {
			obj = manager.getElectronic();
			if(obj!=null){
				list = file.list(obj.getId());
			}
		} catch (ErrMsgException e) {
			e.printStackTrace();
		}
		fm.put("electronic", obj);
		fm.put("list", list);
		fm.put("manager", manager);
		FreeMarkerEngine.action(fm, TemplatePath + "ElectronicEdit.html");
	}
	
	/**
	 * 
	 * 在线投稿列表页面
	 */
	public void ContributeOnline(HttpServletRequest request,
			HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		fm.put("manager", new ContributeManager());
		FreeMarkerEngine.action(fm, TemplatePath + "ContributeOnline.html");
	}
	
	/**
	 * 在线投稿编辑页面
	 * @param request
	 * @param response
	 */
	public void ContributeEdit(HttpServletRequest request,
			HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		ContributeManager manager = new ContributeManager();
		Article obj = new Article();
		try {
			obj = manager.getArticle();
		} catch (Exception e) {
			e.printStackTrace();
		}
		fm.put("article", obj);
		fm.put("photos", new ArticlephotosManager());
		fm.put("files", new ArticlefilesManager());
		FreeMarkerEngine.action(fm, TemplatePath + "ContributeEdit.html");
	}
	
	/**
	 * 
	 * 信息刊用排行榜
	 */
	public void ContributeRank(HttpServletRequest request,
			HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		fm.put("manager", new ContributeManager());
		FreeMarkerEngine.action(fm, TemplatePath + "ContributeRank.html");
	}
}
